package com.hhf.feiojapiservice.config;

import com.hhf.feiojapiservice.fallback.JudgeClientFallbackFactory;
import com.hhf.feiojapiservice.fallback.QuestionClientFallbackFactory;
import com.hhf.feiojapiservice.fallback.UserClientFallbackFactory;
import com.hhf.feiojcommonservice.utils.UserIdContext;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author hhf
 * @Date 2024/11/6
 * @Description
 */
@Configuration
public class DefaultFeignConfig {

    @Bean
    public RequestInterceptor userIdRequestInterceptor(){
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {

                Long userId = UserIdContext.getUserId();

                if (userId == null){
                    return;
                }
                requestTemplate.header("userId",userId.toString());
            }
        };
    }

    @Bean
    public JudgeClientFallbackFactory judgeClientFallbackFactory(){
        return new JudgeClientFallbackFactory();
    }

    @Bean
    public QuestionClientFallbackFactory questionClientFallbackFactory(){
        return new QuestionClientFallbackFactory();
    }

    @Bean
    public UserClientFallbackFactory userClientFallbackFactory(){
        return new UserClientFallbackFactory();
    }

}
